#include "cdaccontroler.h"

CDacControler::CDacControler(QObject *parent) : QSerialPort(parent)
{
    paraInit();
    //connect(this,&CDacControler::readyRead,this,&CDacControler::dealRec);
}

CDacControler::CDacControler(const QSerialPortInfo &serialPortInfo, QObject *parent):QSerialPort(serialPortInfo,parent)
{
    paraInit();
    //connect(this,&CDacControler::readyRead,this,&CDacControler::dealRec);
}

CDacControler::CDacControler(const QString &strPortName, QSerialPort::BaudRate baud, QObject *parent)
{
    this->setParent(parent);
    paraInit();
    this->setPortName(strPortName);
    this->setBaudRate(baud);
    //connect(this,&CDacControler::readyRead,this,&CDacControler::dealRec);
}

CDacControler::~CDacControler()
{
    paraInit();
}

bool CDacControler::set1RoadDacOutput(float volt1)
{
    return set2RoadDacOutput(C1,volt1,0);
}

bool CDacControler::setBatterySimulatorOutput(float volt1, float radio)
{
    return setBatterySimulatorOutput(C1,volt1,0,radio);
}

bool CDacControler::set2RoadDacOutput(ECHANNEL channel,float volt1, float volt2)
{
    qint64 res=0;
    if(!isOpen()) return false;
    int v1=qRound(volt1*100);
    int v2=qRound(volt2*100);
    qDebug()<<"volt1 "<<volt1<<"v1"<< v1;
    QString str1=QString("5A 01 %1 %2 A5").arg(v1/100,2,10,QLatin1Char('0')).arg(v1%100,2,10,QLatin1Char('0'));
    QByteArray cmd1=myHelper::HexStrToByteArray(str1.toLatin1());
    QString str2=QString("5A 02 %1 %2 A5").arg(v2/100,2,10,QLatin1Char('0')).arg(v2%100,2,10,QLatin1Char('0'));
    QByteArray cmd2=myHelper::HexStrToByteArray(str2.toLatin1());
    switch (channel)
    {
    case C1  ://只发生一通道
        if(v1>=0 && v1<1000)  //0000-0999:0V-9.99v
            res=this->write(cmd1);
        break;
    case C2  :
        if(v2>=0 && v2<1000)
            res=this->write(cmd2);
        break;
    case ALL  :
        if(v1>=0 && v1<1000)
            res=this->write(cmd1);
        if(v2>=0 && v2<1000)
            res=this->write(cmd2);
        break;
    default:
        break;
    }

    return res==-1?false:true;
}

bool CDacControler::setBatterySimulatorOutput(ECHANNEL channel, float volt1, float volt2, float radio)
{
    //要输出820V  实际DAC输出820/205=4V
    switch (channel) //检查合法性
    {
    case C1  ://只发生一通道
        if(volt1<0 || volt1>1000) return false;
        break;
    case C2  :
        if(volt2<0 || volt2>1000) return false;
        break;
    case ALL  :
        if(volt1<0 || volt1>1000)  channel=C2;
        if(volt2<0 || volt2>1000)
        {
            if(channel==C2) return false;
            else    channel=C1;
        }
        break;
    default:
        break;
    }

    return set2RoadDacOutput(channel,volt1/radio,volt2/radio);
}
void CDacControler::paraInit()
{
    set1RoadDacOutput(0);
}

void CDacControler::dealRec()
{

}

/*
int a=0001;

QString str = QString("%1").arg(a,4,10,QLatin1Char('0'));


    QByteArray outData=str.toLatin1();
    int size=outData.size();

    if (IsHexSend)//转化为16进制发送
    {
        outData=myHelper::HexStrToByteArray(str);
        size=outData.size();
        myCom->write(outData);
    }

*/
